package com.christian.thread.api;

/**
 * 文件描述：yield 方法，是让出当前线程的执行权，使其他线程执行（可能没有效果）。
 *
 * @Author：wang
 * @Date：2020-01-14 20:55:29
 */
public class YieldCase {

    public static void main(String[] args) {
        Thread t1 = new Thread(() -> {
            for (int i = 0; i < 10; i++) {
                try {
                    Thread.sleep(3 * 100);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }

                System.out.println(Thread.currentThread().getName() + ": 正在运行");
                Thread.yield();
            }
        }, "thread1");

        Thread t2 = new Thread(() -> {
            for (int i = 0; i < 10; i++) {
                try {
                    Thread.sleep(3 * 100);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                System.out.println(Thread.currentThread().getName() + ": 正在运行");
                //调用 yield 方法，让出执行权
//                Thread.yield();
            }
        }, "thread2");

        t1.setPriority(Thread.MIN_PRIORITY);
        t2.setPriority(Thread.MAX_PRIORITY);

        t1.start();
        t2.start();
    }
}
